Ext.ux.ComboBoxCheckTree = function(){
    this.treeId = Ext.id() + '-tree';
    this.maxHeight = arguments[0].maxHeight || arguments[0].height ||
    this.maxHeight;
    this.tpl = new Ext.Template('<tpl for="."><div style="height:' +
    this.maxHeight +
    'px"><div id="' +
    this.treeId +
    '"></div></div></tpl>');
    this.store = new Ext.data.SimpleStore({
        fields: [],
        data: [[]]
    });
    this.selectedClass = '';
    this.mode = 'local';
    this.triggerAction = 'all';
    this.onSelect = Ext.emptyFn;
    this.editable = false;
    this.selectValueModel = 'all';
    Ext.ux.ComboBoxCheckTree.superclass.constructor.apply(this, arguments);
};

Ext.extend(Ext.ux.ComboBoxCheckTree, Ext.form.ComboBox, {
    checkField: 'checked',
    separator: ',',
    initEvents: function(){
        Ext.ux.ComboBoxCheckTree.superclass.initEvents.apply(this, arguments);
        this.keyNav.tab = false;
    },
    initComponent: function(){
        var tree = new Ext.tree.TreePanel({
            rootVisible: true,
            autoHeight: true,
            autoScroll: true,
            border: false,
            loader: new Bee.tree.DWRTreeLoader({
                baseAttrs: {
                    uiProvider: Ext.ux.TreeCheckNodeUI
                },
                dwrCall: this.dwrCall,
                listeners: this.loaderListeners,
                nodeMeta: {
                    id: this.valueField,
                    text: this.displayField,
                    leaf: this.leaf
                }
            }),
            root: new Ext.tree.AsyncTreeNode({
                expandable: true,
                expanded: true,
                text: this.rootText,
                id: this.rootId
            })
        });
        this.tree = tree;
        this.tree.height = this.treeHeight;
        this.tree.containerScroll = false;
        this.on({
            scope: this
        });
    },
    expand: function(){
        Ext.ux.ComboBoxCheckTree.superclass.expand.call(this);
        if (!this.tree.rendered) {
            this.tree.height = this.maxHeight;
            this.tree.border = false;
            this.tree.autoScroll = true;
            if (this.tree.xtype) {
                this.tree = Ext.ComponentMgr.create(this.tree, this.tree.xtype);
            }
            this.tree.render(this.treeId);
            var combox = this;
            this.tree.on('check', function(node, checked){
                combox.setValue();
            });
            var root = this.tree.getRootNode();
            if (!root.isLoaded()) {
                root.reload();
            }
        }
    },
    setChecked: function(ids){
        if (this.tree.rendered) {
            Ext.each(ids, function(id){
                var n = this.tree.getNodeById(id);
                if (n) {
                    var ui = n.getUI();
                    if (ui && ui.setChecked) {
                        ui.setChecked(true);
                    }
                }
            }, this);
        }
    },
    setValue: function(){
        var values = [];
        var texts = [];
        var root = this.tree.getRootNode();
        var checkedNodes = this.tree.getChecked();
        for (var i = 0; i < checkedNodes.length; i++) {
            var node = checkedNodes[i];
            if (this.selectValueModel == 'all' ||
            (this.selectValueModel == 'leaf' && node.isLeaf()) ||
            (this.selectValueModel == 'folder' && !node.isLeaf())) {
                values.push(node.id);
                texts.push(node.text);
            }
        }
        this.value = values.join(this.separator);
        this.setRawValue(texts.join(this.separator));
        if (this.hiddenField) {
            this.hiddenField.value = this.value;
        }
    },
    onViewClick: function(){
    },
    getValue: function(){
        return this.value || '';
    },
    clearValue: function(){
        this.value = '';
        this.setRawValue(this.value);
        if (this.hiddenField) {
            this.hiddenField.value = '';
        }
        this.tree.getSelectionModel().clearSelections();
    }
});

Ext.reg('combochecktree', Ext.ux.ComboBoxCheckTree);
